// formatDate 时间戳格式化
export const formatDate = (date, kind) => {
  kind = kind ? kind : 1
  let yyyy = date.getFullYear()
  let mm = date.getMonth() + 1
  mm = mm < 10 ? ('0' + mm) : mm
  let dd = date.getDate()
  dd = dd < 10 ? ('0' + dd) : dd
  // kind为2时，带时间
  if(kind === 2) {
    let hh = date.getHours()
    hh = hh < 10 ? ('0' + hh) : hh
    let mi = date.getMinutes()
    mi = mi < 10 ? ('0' + mi) : mi
    let ss = date.getSeconds()
    ss = ss < 10 ? ('0' + ss) : ss
    return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + mi + ':' + ss
  }
  return yyyy + '-' + mm + '-' + dd
}

// formatDatetime 时间戳格式化
export const formatDatetime = (date) => {
  let yyyy = date.getFullYear()
  let mm = date.getMonth() + 1
  mm = mm < 10 ? ('0' + mm) : mm
  let dd = date.getDate()
  dd = dd < 10 ? ('0' + dd) : dd
  let hh = date.getHours()
  hh = hh < 10 ? ('0' + hh) : hh
  let mi = date.getMinutes()
  mi = mi < 10 ? ('0' + mi) : mi
  let ss = date.getSeconds()
  ss = ss < 10 ? ('0' + ss) : ss
  return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + mi + ':' + ss
}

// formatDateRange 起止时间戳格式化
export const formatDateRange = (dateArr, kind) => {
  if(dateArr.length === 2 && dateArr[0] && dateArr[1]) {
    kind = kind ? kind : 1
    let beginStr, endStr
    let beginDate = dateArr[0]
    let endDate = dateArr[1]
    let yyyy = beginDate.getFullYear()
    let mm = beginDate.getMonth() + 1
    mm = mm < 10 ? ('0' + mm) : mm
    let dd = beginDate.getDate()
    dd = dd < 10 ? ('0' + dd) : dd
    beginStr = yyyy + '-' + mm + '-' + dd
    yyyy = endDate.getFullYear()
    mm = endDate.getMonth() + 1
    mm = mm < 10 ? ('0' + mm) : mm
    dd = endDate.getDate()
    dd = dd < 10 ? ('0' + dd) : dd
    endStr = yyyy + '-' + mm + '-' + dd
    // kind为2时，带时间
    if(kind === 2) {
      let hh = beginDate.getHours()
      hh = hh < 10 ? ('0' + hh) : hh
      let mi = beginDate.getMinutes()
      mi = mi < 10 ? ('0' + mi) : mi
      let ss = beginDate.getSeconds()
      ss = ss < 10 ? ('0' + ss) : ss
      beginStr = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + mi + ':' + ss
      hh = endDate.getHours()
      hh = hh < 10 ? ('0' + hh) : hh
      mi = endDate.getMinutes()
      mi = mi < 10 ? ('0' + mi) : mi
      ss = endDate.getSeconds()
      ss = ss < 10 ? ('0' + ss) : ss
      endStr = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + mi + ':' + ss
    }
    return beginStr + '~' + endStr
  }
  return ''
}

// maskData2 脱敏，1姓名，2手机号码，3身份证号，4Email，5银行卡号
export const maskData2 = (oldStr, kind) => {
  if(!oldStr) {
    return ''
  }
  let newStr = ''
  switch(kind) {
    case 1:
      // 姓名脱敏
      if(oldStr.length < 3) {
        for(let i=0, c=oldStr.length; i<c; i++) {
          if(i === 0) {
            newStr += oldStr[0]
          } else {
            newStr += '*'
          }
        }
      } else {
        for(let i=0, c=oldStr.length; i<c; i++) {
          if(i === 0) {
            newStr += oldStr[0]
          } else if(i === oldStr.length-1) {
            newStr += oldStr[i]
          } else {
            newStr += '*'
          }
        }
      }
      break
    case 2:
      // 手机号脱敏
      newStr = oldStr.substring(0, 3) + '****' + oldStr.substring(7, 11)
      break
    case 3:
      // 身份证号脱敏
      newStr = oldStr.substring(0, 6) + '********' + oldStr.substring(14, 18)
      break
    case 4:
      // 电子邮箱地址脱敏
      const idx = oldStr.indexOf('@')
      newStr = oldStr.substring(0, 1)
      for(let i=0, c=oldStr.length; i<c; i++) {
        if(i < oldStr.length-idx-2) {
          newStr += '*'
        }
      }
      newStr += oldStr.substring(idx-1, oldStr.length)
      break
    case 5:
      // 银行卡脱敏
      newStr = oldStr.substring(0, 2)
      for(let i=0, c=oldStr.length; i<c; i++) {
        if(i < oldStr.length-4) {
          newStr += '*'
        }
      }
      newStr += oldStr.substring(oldStr.length-2, oldStr.length)
  }
  return newStr
}

// maskData 脱敏，1姓名，2手机号码，3身份证号，4Email，5银行卡号
export const maskData = (oldStr, kind) => {
  if(!oldStr) {
    return ''
  }
  let newStr = ''
  switch(kind) {
    case 1:
      // 姓名脱敏
      if(oldStr.length < 3) {
        newStr += oldStr[0] + '*'
      } else {
        newStr += oldStr[0] + '*' + oldStr[oldStr.length-1]
      }
      break
    case 2:
      // 手机号脱敏
      newStr = oldStr.substring(0, 3) + '****' + oldStr.substring(7, 11)
      break
    case 3:
      // 身份证号脱敏
      newStr = oldStr.substring(0, 6) + '********' + oldStr.substring(14, 18)
      break
    case 4:
      // 电子邮箱地址脱敏
      const idx = oldStr.indexOf('@')
      newStr = oldStr.substring(0, 1) + '****' + oldStr.substring(idx-1, oldStr.length)
      break
    case 5:
      // 银行卡脱敏
      newStr = oldStr.substring(0, 2) + '********' + oldStr.substring(oldStr.length-2, oldStr.length)
  }
  return newStr
}

const testMaskData = () => {
  let oldStr = '张三'
  let newStr = maskData(oldStr, 1)
  console.log('原字符串为：', oldStr)
  console.log('新字符串为：', newStr)
  oldStr = '15904711671'
  newStr = maskData(oldStr, 2)
  console.log('原字符串为：', oldStr)
  console.log('新字符串为：', newStr)
  oldStr = '152631198307280018'
  newStr = maskData(oldStr, 3)
  console.log('原字符串为：', oldStr)
  console.log('新字符串为：', newStr)
  oldStr = 'henry728@163.com'
  newStr = maskData(oldStr, 4)
  console.log('原字符串为：', oldStr)
  console.log('新字符串为：', newStr)
  oldStr = '6229045256481125861'
  newStr = maskData(oldStr, 5)
  console.log('原字符串为：', oldStr)
  console.log('新字符串为：', newStr)
}
